package com.juc.concurrency.basic.yield;

/**
 * @author huangweiyue
 * @description Thread.yield()让出CPU
 * 输出结果分析：
 * 1.注释掉Thread.yield()，大部分情况下同一线程德yield cpu...和is over是一起输出的
 * 2.放开Thread.yield()，大部分情况下会先输出每个线程的yield cpu...，再输出is over
 * @date Created in 2021-07-31
 */
public class YieldExample implements Runnable {
    YieldExample() {
        Thread thread = new Thread(this);
        thread.start();
    }

    public static void main(String[] args) {
        new YieldExample();
        new YieldExample();
        new YieldExample();
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            if (i % 5 == 0) {
                System.out.println(Thread.currentThread() + "yield cpu...");
                Thread.yield();
            }
        }
        System.out.println(Thread.currentThread() + "is over");
    }
}
